package com.hejielife.singlethreadedexecution4;

/**
 * 2019年8月2日 上午10:34:46
 * 
 * @author hejielife
 * @despribe 安全门
 */
public class Gate {
	private int counter = 0;
	private String name = "Nobody";
	private String address = "Nowhere";
	private final Mutex mutex = new Mutex();

	public void pass(String name, String address) { // �ò���synchronized
		mutex.lock();
		try {
			this.counter++;
			this.name = name;
			this.address = address;
			System.out.println("***** pass ***** " + toString());
			check();
		} finally {
			mutex.unlock();
		}
	}

	public String toString() {
		String s = "No." + counter + ": " + name + ", " + address;
		return s;
	}

	private void check() {
		if (name.charAt(0) != address.charAt(0)) {
			System.out.println("***** BROKEN ***** " + toString());
		}
	}
}
